package threads;

import gui.WindowMain;
import resources.FileCleaner;
import resources.PDFTextParser;
import resources.Populator;

public class PDFLoaderThread extends Thread {
	private String fileName;
	private WindowMain window;
	public PDFLoaderThread(WindowMain w,String p){
		fileName=p;
		window=w;
	}
	public void run() {
		//begin the progress bar
		window.addStatusMessage("Processing the selected pdf file");
		window.progressBar.setMaximum(3);
		window.progressBar.setEnabled(true);
		window.progressBar.setVisible(true);
		window.progressBar.setFocusable(true);
		// create the needed objects!
		PDFTextParser parser = new PDFTextParser();
		FileCleaner cleaner = new FileCleaner();
		Populator populator = new Populator();

		String output = "temp.txt";
		// Parses the text from a pdf file to text file.
		window.addStatusMessage("Parsing the pdf file.");
		window.progressBar.setFocusable(true);
		parser.go(fileName, output);
		window.progressBar.setValue(1);
		window.addStatusMessage("Cleaning the pdf file.");
		window.progressBar.setFocusable(true);
		if (cleaner.clean(output)) {
			window.progressBar.setValue(2);
			window.addStatusMessage("Populating data from the pdf file.");
			window.progressBar.setFocusable(true);
			populator.loadData(output);
			window.progressBar.setValue(3);
			

		}
		window.progressBar.setValue(3);
		//close the loading window
		//stop the progress bar
		window.addStatusMessage("The selected pdf file has been processed.");
		window.progressBar.setFocusable(true);
		window.progressBar.setVisible(false);
		window.menuLoadedDBState();
	}
	

}
